﻿using System;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace KouDai.WinPad.Helper
{
    public class FrameStackHelper
    {
        #region Fields

        private int _num = 0;//导航次数的记录器

        #endregion
        

        #region Singleton

        private static FrameStackHelper _instance;

        private FrameStackHelper()
        {

        }

        public static FrameStackHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrameStackHelper();
                }
                return _instance;
            }
        }

        #endregion

        #region Properties

        public int Num
        {
            get { return _num; }
            set { _num = value; }
        }

        #endregion
        
        public void AddCountStack()
        {
            _num++;
        }

        public void RmovePageFromStack()
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                throw new ArgumentNullException("Frame is null.");
            }
            try
            {
                for (int i = 0; i < _num; i++)
                {
                    if (rootFrame.CanGoBack)
                    {
                        rootFrame.GoBack();
                    }
                }
                _num = 0;
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine("navigaion helper exception:" + ex.Message.ToString());
            }
        }
    }
}
